import { recoveryOrder, deleteOrder } from '@/api/order';
import { hasRouteAuth } from '@/utils/permission';
import ElMessage from '@/utils/message';
export default {
  data() {
    return {};
  },
  methods: {
    toRecoveryDialog(id = null) {
      if (!hasRouteAuth('crm:orderList:delete')) {
        ElMessage.warning('暂无还原订单权限，请联系管理人员 ');
        return;
      }
      let ids, isBatch;
      if (id) {
        ids = id;
        isBatch = false;
      } else {
        if (this.selectOrderList.length === 0) {
          ElMessage.warning('请选择要提交的订单');
          return;
        }
        ids = this.selectOrderList.map(i => i.id).join(',');
        isBatch = true;
      }
      this.$confirm('确定恢复订单？', '', {
        confirmButtonText: '确定',
        type: 'warning',
        showClose: false,
        center: true,
        iconClass: 'charge-icon',
        customClass: 'del-message del-message-cancel'
      }).then(() => {
        this.toRecovery(ids, isBatch);
      });
    },
    toRecovery(ids, isBatch) {
      if (!ids || ids === 0) {
        ElMessage.error('请选择订单');
        return;
      }
      recoveryOrder({ ids, isBatch: isBatch }).then(res => {
        if (res.code === 200) {
          ElMessage.success('恢复成功');
          this.clearMixinSelection();
          if (this.fetchData) this.fetchData();
        }
      });
    },
    toDeleteDialog(id = null) {
      if (!hasRouteAuth('crm:orderList:dustbinDelete') && !hasRouteAuth('crm:orderList:dustbinBatchDelete')) {
        ElMessage.warning('暂无删除订单权限，请联系管理人员 ');
        return;
      }
      let ids;
      if (id) {
        ids = `${id}`;
      } else {
        if (this.selectOrderList.length === 0) {
          ElMessage.warning('请选择要删除的订单');
          return;
        }
        ids = this.selectOrderList.map(i => i.id).join(',');
      }
      this.$confirm('确定删除订单？', '', {
        confirmButtonText: '确定',
        type: 'warning',
        showClose: false,
        center: true,
        iconClass: 'charge-icon',
        customClass: 'del-message del-message-cancel'
      }).then(() => {
        this.reallyDelete(ids);
      });
    },
    reallyDelete(ids) {
      if (!ids) {
        ElMessage.error('请选择订单');
        return;
      }
      deleteOrder({ ids }).then(res => {
        if (res.code === 200) {
          ElMessage.success('删除成功');
          this.clearMixinSelection();
          if (this.fetchData) this.fetchData();
        }
      });
    }
  }
};
